package com.qianchang.microfinance.utils;

import android.app.Activity;
import android.content.Context;

import com.qianchang.microfinance.view.MainActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  <描述>
 * 修 改 人:  jiaohongyun
 * 修改时间:  2016/6/20
 */
public class AppManager {
    private static Stack<Activity> activityStack;
    private static AppManager instance;

    private AppManager() {
    }

    /**
     * 单一实例
     */
    public static AppManager getAppManager() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        //		Activity aa = getActivity(activity.getClass());
        //		if (aa != null)
        //		{
        //			//去除多余的实例
        //			finishActivity(aa);
        //		}
        activityStack.add(activity);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity currentActivity() {
        if (activityStack == null || activityStack.size() == 0) {
            return null;
        }
        return activityStack.lastElement();
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishCurrentActivity() {
        if (activityStack != null) {
            Activity activity = activityStack.lastElement();
            finishActivity(activity);
        }
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }


    /**
     * 从任务栈中删除指定的Activity
     */
    public void removeActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        if (activityStack != null) {
            int index = -1;
            for (int i = 0; i < activityStack.size(); i++) {
                if (activityStack.get(i).getClass().equals(cls)) {
                    index = i;
                }
            }
            if (index >= 0) {
                finishActivity(activityStack.get(index));
            }
        }
    }

    /**
     * 返回activity数量
     */
    public int getCount() {
        if (activityStack != null) {
            return activityStack.size();
        } else {
            return 0;
        }

    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if (activityStack != null) {
            for (int i = 0; i < activityStack.size(); i++) {
                if (null != activityStack.get(i)) {
                    finishActivity(activityStack.get(i));
                }
            }
            activityStack.clear();
        }

    }

    /**
     * 结束所有Activity, 除了指定的Activity和MainActivity
     */
    public void finishOtherActivities(Class<?> cls) {
        if (activityStack != null) {
            List<Activity> temp = new ArrayList<Activity>();
            for (int i = 0, size = activityStack.size(); i < size; i++) {
                Class<?> clazz = activityStack.get(i).getClass();
                //MainActivity.class
                if (null != clazz && !clazz.equals(cls) && !clazz.equals(MainActivity.class)) {
                    activityStack.get(i).finish();
                    temp.add(activityStack.get(i));
                }
            }

            for (int i = 0; i < temp.size(); i++) {
                activityStack.remove(temp.get(i));
            }
        }

    }


    /**
     * 结束所有Activity, 除了MainActivity
     */
    public void finishOtherActivities() {
        if (activityStack != null) {
            List<Activity> temp = new ArrayList<Activity>();
            for (int i = 0, size = activityStack.size(); i < size; i++) {
                Class<?> clazz = activityStack.get(i).getClass();
                //MainActivity.class
                if (null != clazz && !clazz.equals(MainActivity.class)) {
                    activityStack.get(i).finish();
                    temp.add(activityStack.get(i));
                }
            }

            for (int i = 0; i < temp.size(); i++) {
                activityStack.remove(temp.get(i));
            }
        }

    }

    /**
     * 获取指定的Activity
     *
     * @author kymjs
     */
    public Activity getActivity(Class<?> cls) {
        if (activityStack != null)
            for (Activity activity : activityStack) {
                if (activity.getClass().equals(cls)) {
                    return activity;
                }
            }
        return null;
    }

    /**
     * 退出应用程序
     */
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            // 杀死该应用进程
//            ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
//            List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses();
//
//            Iterator<ActivityManager.RunningAppProcessInfo> iter = runningAppProcesses.iterator();
//
//            while(iter.hasNext()){
//                ActivityManager.RunningAppProcessInfo next = iter.next();
//
//                String pricessName = context.getPackageName() + ":service";// must set: android:process=":service"
//
//                if(next.processName.equals(pricessName)){
//                    android.os.Process.killProcess(next.pid);
//                    break;
//                }
//            }
//            android.os.Process.killProcess(android.os.Process.myPid());
//            System.exit(0);
        } catch (Exception e) {
        }
    }
}
